package com.example.controller;

import com.example.domain.User;
import com.example.service.UserService;
import com.example.util.VCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    
    /**
     * @param account  获取用户输入的账户
     * @param password 获取用户输入的密码
     * @param vcode    获取用户输入的验证码
     * @param type     获取登录类型
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public String login(String account, String password, String vcode,  HttpServletRequest request) {
        
        //返回信息
        String msg = "";
        
        //获取session中的验证码
        String sVcode = (String) request.getSession().getAttribute("vcode");
        if (!sVcode.equalsIgnoreCase(vcode)) {//先判断验证码是否正确
            msg = "vcodeError";
        } else {    //判断用户名和密码是否正确
            //将账户和密码封装
            User user = new User();
            user.setAccount(account);
            user.setPassword(password);
            
            //创建系统数据层对象
            
            //查询用户是否存在
            //TODO 根据type的不同向不同的service请求登录.
            //type:1-admin,2-student,3-teacher
            User loginUser = userService.getUser(user);
            final int type = loginUser.getType();
            if (loginUser.getId() == null || loginUser.getId() == 0) {//如果用户名或密码错误
                msg = "loginError";
            } else { //正确
                //将该用户名保存到session中
                request.getSession().setAttribute("user", loginUser);
                // if (User.USER_ADMIN == type) {
                //     msg = "admin";
                //     // return "forward:/to/admin";
                // } else if (User.USER_STUDENT == type) {
                //     msg = "student";
                //     // return "forward:/to/student";
                // } else if (User.USER_TEACHER == type) {
                //     msg = "teacher";
                //     // return "forward:/to/teacher";
                // }
            }
        }
        //返回登录信息
        return msg;
    }
    
    /**
     * 获取验证码
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/getVCode")
    public void getVCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建验证码生成器对象
        VCodeGenerator vcGenerator = new VCodeGenerator();
        //生成验证码
        String vcode = vcGenerator.generatorVCode();
        //将验证码保存在session域中,以便判断验证码是否正确
        request.getSession().setAttribute("vcode", vcode);
        //生成验证码图片
        BufferedImage vImg = vcGenerator.generatorRotateVCodeImage(vcode, true);
        //输出图像
        ImageIO.write(vImg, "gif", response.getOutputStream());
    }
}
